
<%@page import="model.Enterprise"%>
<%@page import="model.Employee"%>
<%@page import="model.Person"%>
<%@page import="model.EmployeeRole"%>
<%@page import="model.type.EmployeeRoleType"%>
<%@page import="java.util.ArrayList"%>
<%@page import="web.Html"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<% 
	Enterprise e = (Enterprise)request.getAttribute("enterprise");
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<script src="./script.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>Insert title here</title>
</head>
<body>
	<script>
		window.onload = function(){				
			$('#add_new_role').hide();			
		};
	
		function show(id, hide) {
			$('#' + id).fadeIn();
			$('#' + hide).hide();
		}	
		
		//INSERT NEW ROLE
	    $(function() {  
	        $("#insert_role").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './employee',
	        		  data: $('#role_insert_form').serialize(),
	        		  success : function(){
	        			  location.reload(true);	        			  
	        		  }
	        	});	        	
	        });    
	    }); 
		
	  	//DELETE ROLE
	    function deleteRole(id){	    	 
			$.ajax({
					type : 'POST',
					url : './employee?delete=true',
					data : 'id='+id,
					success : function(data){
						message(data.message, data.classType);
						$('#role' + id).fadeOut();
					},
					dataType: 'json'
			});
		}
	  	
	    function message(string, classType){
			$("#message").hide();
	    	$("#message").fadeIn();
			$("#message").text(string);
			$("#message").removeClass();
			$("#message").addClass(classType);
		}
	</script>	
	<a href="./enterprise?edit=<% out.print(e.getId()); %>"><% out.print(e.getFullName()); %></a>
	<p class="error" id="message"></p>
	<h2>Employee roles</h2>
	<table>		
		<%
		ArrayList<Employee> list = e.getEmployees();		
		if(list.size() == 0) out.println("<tr><td>This enterprise has no employees</td></tr>");
		for(Employee emp: list){			
			for(EmployeeRole role: emp.getRoles()){
				out.println("<tr id=\"role"+role.getId()+"\"><td>"+Html.personLink(emp)+"</td><td>");
				out.println(role.getRoleTypeString()+", ");
				out.println("</td><td><a href=\"#\" class=\"delete\" onclick=\"deleteRole('"+role.getId()+"')\">remove</a></td></tr>");
			}		
		}
		%>		
	</table>
	<div id = "add_new_role">
	<form name="role_insert_form" id="role_insert_form" action ="#">
		<input type="hidden" name ="enterprise" value = <% out.print(e.getId()); %>></input>		
		<table>
		<tr><th>Person</th><th>Role Type</th></tr>
		<tr>
			<td>
				<select name = "person">
					<% 
						ArrayList<Person> personList = (ArrayList<Person>)request.getAttribute("persons");
						for(Person p: personList){
							out.println("<option value = "+p.getId()+">"+p+"</option>");
						}					
					%>									
				</select>
			</td>			
			<td>
				<select name = "role_type">
					<% 
						ArrayList<EmployeeRoleType> roleList = (ArrayList<EmployeeRoleType>)request.getAttribute("roleTypes");
						for(EmployeeRoleType rel: roleList){
							out.println("<option value = "+rel.getId()+">"+rel.getType()+"</option>");
						}	
					%>					
				</select>
			</td>					
		</tr>
		<tr><td><input type="button" id="insert_role" value="Post"></input></td><td></td></tr>
		</table>
	</form>
	</div>
	<a id ="adnewr_but" href="#" onclick="show('add_new_role', 'adnewr_but');">Add new role</a>
</body>
</html>